package com.ywqf_api.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 
 * @author ran
 *
 */
public class CamelNamingUtil {
	private static final char UNDERLINE = '_';
	
	private static final Logger logger = LoggerFactory.getLogger(CamelNamingUtil.class);
	
	/**
	 * 将以驼峰命名法生成的变量名转换成对应的以 "_" 分隔的数据库字段名
	 * @param param
	 * @return
	 */
	public static String camelToUnderline(String param) {
        if (param == null || "".equals(param.trim())) {
            return "";
        }
        int len = param.length();
        StringBuilder sb = new StringBuilder(len);
        for (int i = 0; i < len; i++) {
            char c = param.charAt(i);
            if (Character.isUpperCase(c)) {
                sb.append(UNDERLINE);
                sb.append(Character.toLowerCase(c));
            } else {
                sb.append(c);
            }
        }
        if (logger.isDebugEnabled()) {
			logger.debug("驼峰命名法生成的变量名：" + param + " 被转换成了对应的数据库字段名：" + sb.toString());
		}
        return sb.toString();
    }
}
